package gui.image;

import gui.WrapLayout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import util.MyIniSection;
import util.filefilter.ImageFileFilter;

/**
 *
 * @author Quân
 */
public abstract class ImageList extends javax.swing.JPanel implements Runnable {

    public static final float THUMBNAIL_MAX_SIZE = 100;
    protected List<File> files = new ArrayList<>();
    protected ImagePanel selected = null;
    protected File file;
    protected MyIniSection section;

    public MyIniSection getSection() {
        return section;
    }

    public void setSection(MyIniSection section) {
        this.section = section;
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    /**
     * Creates new form ImageList
     */
    public ImageList(File file, String name) {
        initComponents();
        this.jLabelTitle.setText(name);
        jPanelImage.setLayout(new WrapLayout(WrapLayout.LEFT));
        this.file = file;
    }

    public void scan() {
        File[] images = this.file.listFiles(new ImageFileFilter());
        if (images != null) {
            this.files = Arrays.asList(images);
        }
    }

    @Override
    public void run() {
        if (files == null) {
            return;
        }
        for (int i = 0; i < files.size(); i++) {
            try {
                ImagePanel imagePanel = new ImagePanel(files.get(i));
                this.jPanelImage.add(imagePanel);
                this.jPanelImage.updateUI();
            } catch (IOException ex) {
                Logger.getLogger(ImageList.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void addImage(ImagePanel imagePanel) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, true));
        try (PrintWriter out = new PrintWriter(bufferedWriter)) {
            out.println(imagePanel.getFile().getPath());
        }
        this.jPanelImage.add(imagePanel);
        this.jPanelImage.updateUI();
    }

    /**
     *
     * @param ImagePanelimagePanel
     * @throws IOException
     */
    public abstract void removeImage(ImagePanel imagePanel) throws IOException;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelTitle = new javax.swing.JLabel();
        jPanelImage = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255)));

        jLabelTitle.setText("Title");

        jPanelImage.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanelImage.setLayout(null);
        jPanelImage.setLayout(new WrapLayout(WrapLayout.LEFT));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelTitle)
                        .addGap(0, 561, Short.MAX_VALUE))
                    .addComponent(jPanelImage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelTitle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelImage, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabelTitle;
    protected javax.swing.JPanel jPanelImage;
    // End of variables declaration//GEN-END:variables

    public JPanel getjPanelImage() {
        return jPanelImage;
    }

    public void setjPanelImage(JPanel jPanelImage) {
        this.jPanelImage = jPanelImage;
    }

    public ImagePanel getSelected() {
        return selected;
    }

    public void setSelected(ImagePanel selected) {
        this.selected = selected;
    }

    public List<File> getFiles() {
        return files;
    }

    public void setFiles(List<File> files) {
        this.files = files;
    }
}
